<?php
if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
include_once( "member/proc_betin.php" );
$ui = new UI();
$tpl = new Template2();
$tpl->load(array("news" => "news.htm"));
$conf = new Config();
$news = $conf->Get("news_th");
if ($news == "")
{
    $news = $conf->Get("news");
}
else
{
    $news = "<font color=red>" . $news . "</font>&#12288;" . $conf->Get("news");
}
$tpl->set("newsmessage", $news);
$tpl->show("news");
$tpl->set("account", $curruser['account']);
if ($curruser['enabled'] == 0 || $curruser['enabled'] == 2)
{
    $ui->showErrorPage("<p class=error>帐号已被暂停使用</p>");
    exit();
}
$rs = getdb("select * from lotto where open>='" . date("Y-m-d") . "' order by open");
if ($rs->eof)
{
    $ui->showErrorPage("<p class=error>目前没有可以下注期数！</p>");
    exit();
}
$open = $rs->f['open'];
$round = $rs->f['round'];
$lotto_id = $rs->f['id'];
$othebet = $conf->get("othebet");
$stopbet = $conf->get("stopbet");
$waitbet = $conf->get("waitbet");
if (date("Y-m-d") < $open)
{
    exit();
}
else if (date("Y-m-d") == $open)
{
    if ($stopbet <= date("Hi"))
    {
        $ui->showErrorPage("<p>下注期数：" . d3($round) . "<br>开奖日：" . $open . "</p>" . "<p class=error>已经截止下注！</p>");
        exit();
    }
    else if (date("Hi") <= $conf->get("startbet"))
    {
        exit();
    }
}
$t1 = strtotime($open . " " . $stopbet);
$tpl->Set("timestap", $t1 - systime());
$tpl->set("open", $open);
$tpl->set("round", d3($round));
$tpl->set("lotto_id", $lotto_id);
$tpl->set("account", $curruser['account']);
$tpl->set("amount", $curruser['amount']);
$idisk=getUserValidDisk($curruser);
$disk = $g_disk_code[$idisk];

$rates = array();
$rs0 = getdb("select * from rate0" . $disk . " where lotto_id={$lotto_id}");
if ($rs0->eof)
{
    $ui->showErrorPage("<p class=error>系统目前维护中，请稍後！</p>");
    exit();
}
$rates += $rs0->f;
$rs1 = getdb("select * from rate1" . $disk . " where lotto_id={$lotto_id}");
if ($rs1->eof)
{
    $ui->showErrorPage("<p class=error>系统目前维护中，请稍後！</p>");
    exit();
}
$rates += $rs1->f;
$rs5 = getdb("select * from rate5" . $disk . " where lotto_id={$lotto_id}");
if ($rs5->eof)
{
    $ui->showErrorPage("<p class=error>系统目前维护中，请稍後！</p>");
    exit();
}
$rates += $rs5->f;
$rs15 = getdb("select * from rate15" . $disk . " where lotto_id={$lotto_id} limit 1");
if ($rs15->eof)
{
    $ui->showErrorPage("<p class=error>系统目前维护中，请稍後！</p>");
    exit();
}
$rates += $rs15->f;
$rs15->close();
$rs49 = getdb("select r0 as r49_0,r1 as r49_1,r2 as r49_2,r3 as r49_3,r4 as r49_4 from rate37 where lotto_id={$lotto_id} and bettype=49 and disk=".$idisk." limit 1");
if ($rs49->eof)
{
    $ui->showErrorPage("<p class=error>系统目前维护中，请稍後！</p>");
    exit();
}
$rates += $rs49->f;
$rs49->close();
$rs50 = getdb("select r0 as r50_0,r1 as r50_1,r2 as r50_2,r3 as r50_3,r4 as r50_4,r5 as r50_5,r6 as r50_6,r7 as r50_7,r8 as r50_8,r9 as r50_9 from rate37 where lotto_id={$lotto_id} and bettype=50 and disk=".$idisk." limit 1");
if ($rs50->eof)
{
    $ui->showErrorPage("<p class=error>系统目前维护中，请稍後！</p>");
    exit();
}
$rates += $rs50->f;
$rs50->close();
$rs->close();
$betmin = $conf->get("betmin");
$tpl->set("min", $betmin);
$betmax = $conf->get("betmax");
$tpl->set("max", $betmax);
$tpl->set("max_str", $betmax);
$rs = getdb("select amount_6h from k_user_bet where user_id=" . $curruser['id'] . " and lotto_id=" . $lotto_id);
$usedamount = $rs->f[0] ? $rs->f[0] : 0;
$rs->close();
$limit = array();
$limit[0] = GetAmountInfo(0);
$limit[1] = GetAmountInfo(1);
$limit[2] = GetAmountInfo(2);
$limit[3] = GetAmountInfo(3);
$limit[5] = GetAmountInfo(5);
$limit[6] = GetAmountInfo(6);
$limit[13] = GetAmountInfo(13);
$limit[14] = GetAmountInfo(14);
$limit[20] = GetAmountInfo(20);
$limit[24] = GetAmountInfo(24);
$limit[28] = GetAmountInfo(28);
$limit[30] = GetAmountInfo(30);
$limit[46] = GetAmountInfo(46);
$limit[47] = GetAmountInfo(47);
$limit[15] = GetAmountInfo(15);
$limit[49] = GetAmountInfo(49);
$limit[50] = GetAmountInfo(50);
$ok = isset($_POST['ok']);
if ($ok)
{
    echo "<body>";
}
else
{
    echo "<body onload='StartFocus()'>";
}
echo "<form action=\"op.php?op=member&fp=proc0".(empty($_GET['back'])?"":"&back=".$_GET['back'])."\" method=post onsubmit=\"this.submit.disabled=true;this.reset.disabled=true\">";
echo "<input type=hidden name=ok value=1>";
echo "<input type=hidden name=disk value=" . strpos(" abcd", $disk) . ">";
$waittime = 2000;
$betcount = 0;
$total = 0;
$tmp = "";
$oddname = array("", "单", "双");
$bigname = array("", "大", "小");
$bigmidsmall = array("", "大", "中", "小");
$chinayearname = array("", "家禽", "野兽");
$colorname = array("", "红波", "蓝波", "绿波");
$zmnum = array("", "一", "二", "三", "四", "五", "六");
$usemaxamount = $curruser['usemaxamount'];
$user_rate0 = get_share_rate0($curruser['id']);
$rate0_8num = get_share_rate0_8($curruser['id']);
$user_rate8 = $user_rate0 * $rate0_8num;
$u2 = get_user($curruser['prev_id']);
$u3 = get_user($u2['prev_id']);
$u4 = get_user($u3['prev_id']);
$u5 = get_user($u4['prev_id']);
foreach ($_POST as $key => $val)
{
    $val = intval($val);
    if (0 < $val)
    {
        $p = explode("_", $key);
		
        if ($key != "ok" && 0 < count($p))
        {
            $p[0] = substr($p[0], 1);
            if (isset($p[0], $p[1]))
            {
                $tmp .= "<tr class=td2>";
                $tmp .= "<td>" . ($p[0]==15?'正码'.$zmnum[$p[1]]:$g_betname[$p[0]]) . "</td>";
                $tmp .= "<td>";
                ++$betcount;
                $max_rate = 0;

                switch ($p[0])
                {
                    case 13 :
                        $tmp .= $colorname[$p[1]];
                        $max_rate = 3.2;
                        break;
                    case 46 :
                        $tmp .= $bigmidsmall[$p[1]];
                        $max_rate = 3.2;
                        break;
                    case 14 :
                        $tmp .= $g_chinayear[$p[1]];
                        $max_rate = 14;
                        break;
                    case 0 :
                        $tmp .= $p[1];
                        $max_rate = 55;
                        break;
                    case 2 :
                    case 6 :
                    case 28 :
                    case 30 :
                    case 47 :
                        $tmp .= $bigname[$p[1]];
                        $max_rate = 2.6;
                        break;
                    case 20 :
                        $tmp .= $chinayearname[$p[1]];
                        $max_rate = 2.6;
                        break;
                    case 1 :
                    case 3 :
                    case 5 :
                        $tmp .= $oddname[$p[1]];
                        $max_rate = 2.6;
                        break;
					case 15:
						$tmp.=getZmName($p[2]);
						$max_rate = 2.6;
						break;
                    default :
                        $tmp .= $p[1];
                        $max_rate = 55;
                        break;
                }
                $tmp .= "</td>";
				if($p[0]==15)
				{
					$sub_rates = get_subs_rates($curruser['id'], $lotto_id, $bettype, $num . "_" . $type);
					$rate = $rates[$key] - $sub_rates - 0;		
				}
				elseif($p[0]==49 || $p[0]==50)
				{
					$sub_rates=0;
					$rate = $rates[$key];		
				}
				else{
                	$sub_rates = get_subs_rates($u5['id'], $lotto_id, $p[0], $p[1]);
                	$rate = $rates["r" . ( $p[0] == 0 ? $p[1] : ($p[0] . "_" . $p[1]))] - $sub_rates;
					if ($p[0] == 0)
					{
						$rate -= $user_rate0;
					}
					else if (0 < $p[0] && $p[0] < 4 || $p[0] == 20 || $p[0] == 28 || $p[0] == 30)
					{
						$rate -= $user_rate8;
					}
				}
                $tmp .= "<td align=right>" . trimfloat($rate, 1) . "</td>";
                $tmp .= "<td align=right>" . $val . "</td>";
                if ($ok)
                {
                    $tmp .= "<td>";
                    $betallow = $limit[$p[0]][2];
                    $oneamount = $limit[$p[0]][1];
                    if ($p[0] == 0 || $p[0] == 24)
                    {
                        $maxamount = get_bet0_money(0, $p[1]);
                        if (0 < $maxamount)
                        {
                            $oneamount = min($oneamount, $maxamount);
                        }
                    }
                    if ($val < $betmin)
                    {
                        $tmp .= "<font color=red>小於最小限额";
                        $waittime += 200;
                    }
                    else if ($curruser['balance'] < $val)
                    {
                        $tmp .= "<font color=red>超过信用额度";
                        $waittime += 200;
                    }
                    else if ($usemaxamount && $curruser['maxamount'] < $val + $usedamount)
                    {
                        $tmp .= "<font color=red>超过本期限额";
                        $waittime += 200;
                    }
					 else if ($usemaxamount && $curruser['maxamount'] < $val + $usedamount)
                    {
                        $tmp .= "<font color=red>超过本期限额";
                        $waittime += 200;
                    }
                    else if ($oneamount < $val)
                    {
                        $tmp .= "<font color=red>超过单注限额";
                        $waittime += 200;
                    }
                    else if ($rate <= 0 || $max_rate < $rate)
                    {
                        $tmp .= "<font color=red>赔率数值错误";
                        $waittime += 200;
                    }
                    else
                    {
                        //新增
						if($p[0]==15)
										{
											switch ($p[2])
											{
												case 1 :
												case 2 :
													$p[0] = 15;
													break;
												case 3 :
												case 4 :
													$p[0] = 16;
													break;
												case 5 :
												case 6 :
												case 7 :
													$p[0] = 17;
													break;
												case 8 :
												case 9 :
													$p[0] = 23;
													break;
											}
										}
								$rs1 = getdb( "select * from amounts where bettype=" . $p[0] . " and user_id=".$curruser['id'] );
								$amount1 = $rs1->f['amount1'] - 0;//每注限额
								$amount2 = $rs1->f['amount2'] - 0;//每期每注下注总额
								$bet = 0;
                                $rs1 = getdb("select sum(amount) as amount,numbers from bet where bettype=" . $p[0] . " and user_id=" . $curruser['id'] . " and lotto_id=" . $lotto_id . " and rame=0 group by numbers");
                                while (!$rs1->eof)
                                {
                                    
									if($p[0]==15 || $p[0]==17 || $p[0]==23){
										if ($rs1->f['numbers'] == $p[1]." ".$p[2])
										{
											$bet += $rs1->f['amount'];//已下注每期总额
										}
									
									}
									
									elseif ($rs1->f['numbers'] == $p[1])
                                    {
                                        $bet += $rs1->f['amount'];//已下注每期总额
                                    }
                                    
                                    $rs1->movenext();
                                }
								
						//结束新增
						$err = 0;
                        switch ($p[0])
                        {
                            case 0 :
                                $betallow = $amount2 - $bet;
								
                                if ($amount1 < $val)
                                {
                                    $err = 1;
                                }elseif($betallow< $val){
									 $err = 2;
								}
                                else if ($othebet <= date("Hi"))
                                {
                                    $err = 3;
                                }
                                break;
							
                            case 4 :
                                $n = getnumberamount($curruser['id'], $lotto_id, $p[0], intval($p[1]));
                                if ($limit[$p[0]][2] < $val + $n)
                                {
                                    $err = 1;
                                }
                                break;
                            case 1 ://两面
								$betallow = $amount2 - $bet;
								
                                if ($amount1 < $val)
                                {
                                    $err = 1;
                                }elseif($betallow< $val){
									 $err = 2;
								}
                                else if ($othebet <= date("Hi"))
                                {
                                    $err = 3;
                                }
                                break;
							
                            case 2 :
                            case 3 :
								$betallow = $amount2 - $bet;
								
                                if ($amount1 < $val)
                                {
                                    $err = 1;
                                }elseif($betallow< $val){
									 $err = 2;
								}
                                else if ($othebet <= date("Hi"))
                                {
                                    $err = 3;
                                }
                                break;
                            case 5 :
                            case 6 :
                            case 20 :
                            case 28 :
								$betallow = $amount2 - $bet;
								
                                if ($amount1 < $val)
                                {
                                    $err = 1;
                                }elseif($betallow< $val){
									 $err = 2;
								}
                                else if ($othebet <= date("Hi"))
                                {
                                    $err = 3;
                                }
                                break;
							case 48 :
								$betallow = $amount2 - $bet;
								
                                if ($amount1 < $val)
                                {
                                    $err = 1;
                                }elseif($betallow< $val){
									 $err = 2;
								}
                                else if ($othebet <= date("Hi"))
                                {
                                    $err = 3;
                                }
                                break;
                            case 30 :
                            case 47 :
                                $betallow = $amount2 - $bet;
								
                                if ($amount1 < $val)
                                {
                                    $err = 1;
                                }elseif($betallow< $val){
									 $err = 2;
								}
                                else if ($othebet <= date("Hi"))
                                {
                                    $err = 3;
                                }
                                break;
							case 49 :
									$rs1 = getdb( "select * from amounts where bettype=" . $p[0] . " and user_id=".$curruser['id'] );
									$amount1 = $rs1->f['amount1'] - 0;//每注限额
									$amount2 = $rs1->f['amount2'] - 0;//每期每注下注总额  
                                $useAmt = Get_bet_amounts($p[0], $p[1]);
							
                                $betallow = $amount2 - $useAmt;
                                if ($betallow < $val)
                                {
                                    $err = 2;
                                }
                                if ($othebet <= date("Hi"))
                                {
                                    $err = 3;
                                }
                                if ($stopbet <= date("Hi") && $err == 0)
                                {
                                    $err = 3;
                                }
                                break;
                            case 15 :
							case 16 :
							case 17 :
							case 23 :
								
                                $betallow = $amount2 - $bet;
                                if ($amount1 < $val)
                                {
                                    $err = 1;
                                }elseif($betallow< $val){
									 $err = 2;
								}
                                else if ($othebet <= date("Hi"))
                                {
                                    $err = 3;
                                }
                                break;
                            case 46 :
                            case 13 :
                            case 14 :
                                $useAmt = Get_bet_amounts($p[0], $p[1]);
                                $betallow = $betallow - $useAmt;
                                if ($betallow < $val)
                                {
                                    $err = 1;
                                }
                                if ($othebet <= date("Hi"))
                                {
                                    $err = 3;
                                }
                                if ($stopbet <= date("Hi") && $err == 0)
                                {
                                    $err = 3;
                                }
                                break;
								case 50 :
									$rs1 = getdb( "select * from amounts where bettype=" . $p[0] . " and user_id=".$curruser['id'] );
									$amount1 = $rs1->f['amount1'] - 0;//每注限额
									$amount2 = $rs1->f['amount2'] - 0;//每期每注下注总额  
                                $useAmt = Get_bet_amounts($p[0], $p[1]);
							
                                $betallow = $amount2 - $useAmt;
                                if ($betallow < $val)
                                {
                                    $err = 2;
                                }
                                if ($othebet <= date("Hi"))
                                {
                                    $err = 3;
                                }
                                if ($stopbet <= date("Hi") && $err == 0)
                                {
                                    $err = 3;
                                }
                                break;
                        }
                        if ($err == 1)
                        {
                            $tmp .= "<font color=red>超过下注限额";
                            $waittime += 200;
                        }
                        else if ($err == 2)
                        {
                            $tmp .= "<font color=red>超过下注总额";
                            $waittime += 200;
                        }
                        else if ($err == 3)
                        {
                            $tmp .= "<font color=red><b>已经截止下注</b>";
                            $waittime += 200;
                        }
                        else
                        {
                            $stime = systime();
							if($p[0]==15)
							{
								$p[1].=" ".$p[2];	
								switch ($p[2])
								{
									case 1 :
									case 2 :
										$p[0] = 15;
										break;
									case 3 :
									case 4 :
										$p[0] = 16;
										break;
									case 5 :
									case 6 :
									case 7 :
										$p[0] = 17;
										break;
									case 8 :
									case 9 :
										$p[0] = 23;
										break;
								}
							}
                            if (betin($p, $val, $rate, $sub_rates, $u2, $u3, $u4, $u5, $stime, $disk))
                            {
                                $tmp .= "<font color=green>成功 ";
                                $usedamount += $val;
                                $total += $val;
                            }
                            else
                            {
                                $tmp .= "<font color=red>超过信用额度";
                                break;
                            }
                        }
                    }
                    $tmp .= "</font></td>";
                }
                else if ($p[0] != "")
                {
                    echo "<input type=hidden name=\"" . $key . "\" value=\"" . $val . "\">";
                    $total += $val;
                }
                $tmp .= "</tr>";
            }
        }
    }
}
if ($betcount == 0)
{
    $ui->showErrorPage("<p>请输入下注金额！</p>" . ( "<p><input type=button value='回上一页' onclick='history.back()'></p>" ));
    exit();
}
echo "您下注的是：" . strtoupper($disk) . " 盘";
echo "<table border=0 cellpadding=2 cellspacing=1 class=table0>";
echo "<tr class=td0><td>下注方式</td><td>下注内容</td><td>赔率</td><td>下注金额</td>";
if ($ok)
{
    echo "<td>结果</td>";
}
echo "</tr>";
echo $tmp;
echo "<tr class=td0 align=right><td colspan=3>合计</td><td>" . $total . "</td>" . ( $ok ? "<td>&nbsp;</td>" : "" ) . "</tr>";
echo "</table>";
if ($ok)
{
    echo "<script>";
    echo "parent.leftFrame.location.href='op.php?op=member&fp=account';";
    echo "setTimeout(\"location.href='op.php?op=member&fp=".(empty($_GET['back'])?"bet0":$_GET['back'])."'\"," . $waittime . ");";
    echo "</script>";
}
else
{
    echo "<br>";
    echo "<input type=submit name=submit value=确定下注>&#12288;<input name=reset type=reset value=取消下注 onclick='history.back()'>";
    echo "</form>";
    echo "<Script>function StartFocus(){  document.forms[0].submit.focus();}</Script>";
    echo "</body>";
}
function getZmName($type)
{
	$betname=$type;
	switch ($type)
	{
		case 1 :
			$betname = "单";
			break;
		case 2 :
			$betname = "双";
			break;
		case 3 :
			$betname = "大";
			break;
		case 4 :
			$betname = "小";
			break;
		case 5 :
			$betname = "红";
			break;
		case 6 :
			$betname = "蓝";
			break;
		case 7 :
			$betname = "绿";
			break;
		case 8 :
			$betname = "合单";
			break;
		case 9 :
			$betname = "合双";
	}	
	return $betname;
}
?>